package cn.interestingshop.servlet;

import cn.interestingshop.model.entity.User;
import cn.interestingshop.service.UserService;
import cn.interestingshop.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "UserServlet", urlPatterns = "/UserServlet")
public class UserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    UserService service = new UserServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("utf-8");

        String action = request.getParameter("action");

        switch (action) {
            case "login" :
                login(request, response);
                break;
            case "register" :
                register(request, response);
                break;
            case "quit" :
                quit(request, response);
                break;
        }
    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String account = request.getParameter("account");
        String password = request.getParameter("password");

        User user = service.findUserByAccount(account);

        String[] remember = request.getParameterValues("remember");

        if (remember != null) {
            Cookie cookie1 =  new Cookie("account", account);
            Cookie cookie2 = new Cookie("password", password);

            cookie1.setMaxAge(60 * 60);
            cookie2.setMaxAge(60 * 60);

            response.addCookie(cookie1);
            response.addCookie(cookie2);
        } else {
            Cookie[] cookies = request.getCookies();

            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(account)) {
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }

                if (cookie.getName().equals(password)) {
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }
            }
        }

        String msg = "";

        if (user == null) {
            msg = "账号不存在";
        } else if (!password.equals(user.getPassword())) {
            msg = "密码不正确";
        } else {
            msg = "登录成功";

            request.getSession().setAttribute("loginUser", user);
        }

        PrintWriter out = response.getWriter();

        out.print(msg);
        out.flush();
        out.close();
    }

    public void quit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().removeAttribute("loginUser");
        response.sendRedirect("client/Login.jsp");
    }

    public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        String nickName = request.getParameter("nickName");
        Integer gender = Integer.valueOf(request.getParameter("gender"));
        String idCard = request.getParameter("idCard");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");

        User user = new User(null, account, nickName, password, gender, idCard, email, phone, null, null);

        UserService service = new UserServiceImpl();

        int ret = service.add(user);

        PrintWriter out = response.getWriter();

        if (ret > 0) {
            out.print("<script>alert(\"注册成功，请登录\");location.href='client/Login.jsp';</script>");
        } else {
            out.print("<script>alert(\"注册失败，请重新填写信息\");</script>");
        }

        out.flush();
        out.close();
    }
}
